package com.joinway.framework.admin.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;

import org.hibernate.validator.constraints.NotBlank;
import org.jsondoc.core.annotation.Api;
import org.jsondoc.core.annotation.ApiMethod;
import org.jsondoc.core.annotation.ApiQueryParam;
import org.jsondoc.core.annotation.ApiResponseObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.joinway.framework.admin.bean.UserContext;
import com.joinway.framework.admin.core.constant.AdminWebConstants;
import com.joinway.framework.admin.core.service.AdminMessageService;
import com.joinway.framework.bean.constant.ErrorCodeConstants;
import com.joinway.framework.bean.logging.annotation.InputLog;
import com.joinway.framework.bean.logging.annotation.LogIgnore;
import com.joinway.framework.bean.logging.annotation.OutputLog;
import com.joinway.framework.bean.security.annotation.Security;
import com.joinway.framework.bean.view.View;
import com.joinway.framework.core.controller.ExceptionController;
import com.joinway.framework.extension.utils.NumberUtils;

/**
 * @author Administrator
 *
 */
@Api(name = "管理后台推送接口", description = "")
@RestController
@RequestMapping("push")
@Validated
@Security
public class AdminMessageController extends ExceptionController {

	private final static Logger log = LoggerFactory.getLogger(AdminMessageController.class);
	
	@Autowired AdminMessageService service;
	
	@ApiMethod(description="广播消息", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/broadcast.json", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@InputLog
	@OutputLog
	public View broadcast(
		@RequestParam("title") @NotBlank @ApiQueryParam(name="title", description="消息标题") String title,
		@RequestParam("text") @NotBlank @ApiQueryParam(name="text", description="消息文本") String text,
		@RequestParam("html") @NotBlank @ApiQueryParam(name="html", description="html文本") String html,
		@RequestParam(value="port",required=false,defaultValue="80") @ApiQueryParam(name="port", description="端口") String port,
		@LogIgnore HttpServletRequest request
	) throws Exception {
		UserContext context = (UserContext)request.getSession().getAttribute(AdminWebConstants.USER_CONTEXT_KEY);
		
		boolean ret = service.push(title, text, html, NumberUtils.convertToInt(port), context.getUser());
		
		View view = new View();
		if(!ret){
			view.setCode(ErrorCodeConstants.REQUEST_TIMEOUT);
			view.setError("broadcast with error");
		}
		
		return view;
	}

	@ApiMethod(description="群发消息", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/mass.json", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@InputLog
	@OutputLog
	public View massPush(
		@RequestParam("title") @NotBlank @ApiQueryParam(name="title", description="消息标题") String title,
		@RequestParam("text") @NotBlank @ApiQueryParam(name="text", description="消息文本") String text,
		@RequestParam("html") @NotBlank @ApiQueryParam(name="html", description="html文本") String html,
		@RequestParam("toUserIds") @NotBlank @ApiQueryParam(name="toUserIds", description="目标用户id") String toUserIds,
		@RequestParam(value="port",required=false,defaultValue="80") @ApiQueryParam(name="port", description="端口") String port,
		@LogIgnore HttpServletRequest request
	) throws Exception {
		UserContext context = (UserContext)request.getSession().getAttribute(AdminWebConstants.USER_CONTEXT_KEY);
		
		return service.massPush(title, text, html, toUserIds.split(","), NumberUtils.convertToInt(port), context.getUser());
	}
	
	@ApiMethod(description="重发消息", produces=MediaType.APPLICATION_JSON_VALUE)
	@ApiResponseObject
	@RequestMapping(value="/resend.json", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	@InputLog
	@OutputLog
	public View resendPush(
		@RequestParam("toUserIds") @NotBlank @ApiQueryParam(name="toUserIds", description="目标用户id") String toUserIds, 
		@RequestParam("pageId") @Min(1) @ApiQueryParam(name="pageId", description="重发的网页id") int pageId,
		@RequestParam(value="port",required=false,defaultValue="80") @ApiQueryParam(name="port", description="端口") String port,
		@LogIgnore HttpServletRequest request
	) throws Exception {
		UserContext context = (UserContext)request.getSession().getAttribute(AdminWebConstants.USER_CONTEXT_KEY);
		
		return service.resendPush(toUserIds.split(","), pageId, port, context.getUser());
	}

}


